//
//  MenuMediator.m
//  MoneyIndividual
//
//  Created by Le Anh Tai on 12/26/13.
//  Copyright (c) 2013 Le Anh Tai. All rights reserved.
//

#import "MenuMediator.h"
#import "MenuController.h"
#import "SlidingViewController.h"
#import "HomeController.h"
#import "TransactionBookController.h"
#import "LoanController.h"
#import "DebtController.h"
#import "HuiSummaryController.h"
#import "ChartController.h"
#import "SettingsController.h"
#import "Utility.h"

@implementation MenuMediator

static MenuMediator *_shared;
+ (MenuMediator *)shared
{
    if (_shared == nil)
    {
        _shared = [[MenuMediator alloc] init];
    }
    
    return _shared;
}

- (id)init
{
    if (self = [super init])
    {
        _screenName = ScreenNameNone;
        _slidingController = [[SlidingViewController alloc] init];
    }
    
    return self;
}

- (void)openScreen:(ScreenName)screenName IsSelectMenuItem:(BOOL)isSelect
{
    if (isSelect)
    {
        MenuController *menuViewController = ((UINavigationController*)[_slidingController underLeftViewController]).viewControllers[0];
        if (menuViewController && [menuViewController isKindOfClass:[MenuController class]])
        {
            [menuViewController selectMenuItem:screenName];
        }
    }
    else
    {
        [self openScreen:screenName];
    }
}

- (void)openScreen:(ScreenName)name
{
    if (name == _screenName)
    {
        return;
    }
    
    [self removeCurrentView:name];
    
    switch (name)
    {
        case ScreenNameHomeController:
        {
            [self openHomeController];
            break;
        }
            
        case ScreenNameTransactionBookController:
        {
            [self openTransactionBookController];
            break;
        }
            
        case ScreenNameLoanController:
        {
            [self openLoanController];
            break;
        }
            
        case ScreenNameDebtController:
        {
            [self openDebtController];
            break;
        }
            
        case ScreenNameHuiSummaryController:
        {
            [self openHuiSummaryController];
            break;
        }
        case ScreenNameChartController:
        {
            [self openChartController];
            break;
        }
            
        case ScreenNameSettingViewController:
        {
            [self openSettingController];
            break;
        }
            
        default:
            break;
    }
    
    _screenName = name;
    
    if (self.curNavigationController && self.curNavigationController.view.superview == nil)
    {
        [_slidingController setTopViewController:self.curNavigationController];
    }
}

- (void)removeCurrentView:(ScreenName) newScreenName
{
    UIView *preView = nil;
    
    //clearn resource previous view
    if (self.curNavigationController)
    {
        [self.curNavigationController removeFromParentViewController];
        preView = self.curNavigationController.view;
        
        if (preView.superview)
        {
            [preView removeFromSuperview];
        }
    }
}

- (void)clear
{
    [self removeCurrentView:ScreenNameNone];
    self.curNavigationController = nil;
}

#pragma mark - Open Controllers

- (void)openHomeController
{
    HomeController *homeController = [[HomeController alloc] initAsRootOfNavigationController];
    UINavigationController *navigationController = [UINavigationController newNavigationWithRoot:homeController];

    self.curNavigationController = navigationController;
}

- (void)openTransactionBookController
{
    TransactionBookController *transactionBookController = [[TransactionBookController alloc] initAsRootOfNavigationController];
    UINavigationController *navigationController = [UINavigationController newNavigationWithRoot:transactionBookController];
    
    self.curNavigationController = navigationController;
}

- (void)openLoanController
{
    LoanController *loanController = [[LoanController alloc] initAsRootOfNavigationController];
    UINavigationController *navigationController = [UINavigationController newNavigationWithRoot:loanController];
    
    self.curNavigationController = navigationController;
}

- (void)openDebtController
{
    DebtController *debtController = [[DebtController alloc] initAsRootOfNavigationController];
    UINavigationController *navigationController = [UINavigationController newNavigationWithRoot:debtController];
    
    self.curNavigationController = navigationController;
}

- (void)openHuiSummaryController
{
    HuiSummaryController *huiController = [[HuiSummaryController alloc] initAsRootOfNavigationController];
    UINavigationController *navigationController = [UINavigationController newNavigationWithRoot:huiController];
    
    self.curNavigationController = navigationController;
}

- (void)openChartController
{
    ChartController *chartController = [[ChartController alloc] initAsRootOfNavigationController];
    UINavigationController *navigationController = [UINavigationController newNavigationWithRoot:chartController];
    
    self.curNavigationController = navigationController;
}

- (void)openSettingController
{
    SettingsController *settingController = [[SettingsController alloc] initAsRootOfNavigationController];
    UINavigationController *navigationController = [UINavigationController newNavigationWithRoot:settingController];
    
    self.curNavigationController = navigationController;
}

@end
